import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
  OnInit,
  AfterViewInit,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PlanCategory,
  ComboboxItemDto,
  GetTabDtosForTabOutput,
  MonthPlanServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import * as moment from "moment";
import { finalize } from "rxjs/operators";

@Component({
  templateUrl: "./ready.component.html",
  styleUrls: ["./ready.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class ReadyComponent extends AppComponentBase implements AfterViewInit {
  @ViewChild("dataTable") dataTable: Table;
  @ViewChild("paginator") paginator: Paginator;

  @ViewChild("dateTimePicker") dateTimePicker: ElementRef;

  @ViewChild("orderCategoryCombobox") orderCategoryCombobox: ElementRef;
  @ViewChild("teamGroupCombobox") teamGroupCombobox: ElementRef;

  tabType = 1;

  tabs: GetTabDtosForTabOutput[];
  advancedFiltersAreShown = false;

  anyncing = false;
  //Filters
  filterText = "";

  planDate = new Date();

  constructor(
    injector: Injector,
    private _monthServiceProxy: MonthPlanServiceProxy
  ) {
    super(injector);
  }

  ngAfterViewInit() {
    this.getTabs();
    this.getMonths();
  }

  tabTypeClick(tabType: number) {
    this.tabType = tabType;
    this.getMonths();
  }

  getTabs() {
    this._monthServiceProxy
      .getTabDtosForTab(
        this.planDate.getFullYear(),
        this.planDate.getMonth() + 1
      )
      .subscribe((result) => {
        this.tabs = result.items;
      });
  }

  getMonths(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._monthServiceProxy
      .getReadMonthPlans(
        2,
        this.tabType,
        this.planDate.getFullYear(),
        this.planDate.getMonth() + 1,
        this.filterText,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe((result) => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        console.log(this.primengTableHelper.records);
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  importMonthReady() {
    this.message.confirm(
      "确定同步该月的生产准备吗？",
      this.planDate.getFullYear() +
        "年" +
        (this.planDate.getMonth() + 1) +
        "月",
      (isConfirmed) => {
        if (isConfirmed) {
          this.anyncing = true;
          this._monthServiceProxy
            .batchBoundTech(
              this.planDate.getFullYear(),
              this.planDate.getMonth() + 1
            )
            .pipe(
              finalize(() => {
                this.anyncing = false;
              })
            )
            .subscribe(() => {
              this.notify.success("同步月度计划成功");
              this.getMonths();
            });
        }
      }
    );
  }
}
